using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CocaCola_Inventory_System.Forms
{
    public partial class detailManager : Form
    {
        bool modified = false;
        public detailManager()
        {
            InitializeComponent();
        }

        private void btn_insert_Click(object sender, EventArgs e)
        {
            insertDetail t = new insertDetail();
            //t.MdiParent = this.MdiParent;
            t.con = ((CocaCola_Inventory_System.Forms.Container)this.MdiParent).con;
            t.ShowDialog();
            btn_refresh.PerformClick();
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            if (grd_categories.SelectedRows.Count > 0)
            {
                if (grd_categories.SelectedRows.Count > 1)
                {
                    if (MessageBox.Show("Are you sure you want to delete the selected rows?", "Delete Rows", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                    {
                        int ldeleted = -1;
                        Stack<DataGridViewRow> dels = new Stack<DataGridViewRow>(grd_categories.SelectedRows.Count);
                        foreach (DataGridViewRow x in grd_categories.SelectedRows)
                        {
                            if (x.Index != ldeleted)
                            {
                                dels.Push(x);
                                ldeleted = x.Index;
                            }
                        }

                        while (dels.Count > 0)
                        {
                            grd_categories.Rows.Remove(dels.Pop());
                        }
                    }
                }
                else
                {
                    string nm = (string)grd_categories[1, grd_categories.SelectedRows[0].Index].Value;
                    if (MessageBox.Show("Are you sure you want to delete \"" + nm + "\"?", "Delete " + nm, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                        grd_categories.Rows.Remove(grd_categories.SelectedRows[0]);
                }
            }
            else
            {
                MessageBox.Show("You have to select a row first", "Delete Entry", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void btn_refresh_Click(object sender, EventArgs e)
        {
            if (modified)
            {
                if (MessageBox.Show("Are you sure you wish to discard the changes?", "Save Changes", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    grd_categories.DataSource = Data_Access.detailDB.getDetails(((CocaCola_Inventory_System.Forms.Container)this.MdiParent).con);
                    grd_categories.Refresh();
                }
            }
            else
            {
                grd_categories.DataSource = Data_Access.detailDB.getDetails(((CocaCola_Inventory_System.Forms.Container)this.MdiParent).con);
                grd_categories.Refresh();
            }
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Deleting parent detail types will also delete children details,Are you sure you want to apply all changes permanently?", "Save Changes", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                Data_Access.detailDB.updateDetails(((CocaCola_Inventory_System.Forms.Container)this.MdiParent).con, (DataTable)grd_categories.DataSource);
                modified = false;
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void detailManager_Load(object sender, EventArgs e)
        {
            btn_refresh.PerformClick();
        }

        private void detailManager_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (modified)
            {
                if (MessageBox.Show("Are you sure you wish to discard the changes?", "Save Changes", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.No)
                {
                    e.Cancel = true;
                }
            }
        }

        private void grd_categories_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            modified = true;
        }

    }
}